/*
 * trans_p_ps.c
 *
 * Created: 2013-01-08 13:00:11
 *  Author: ant 
 */ 
#include "../sc-p/trans-p.h"
#include "../sc-p/utils.h"
#include "sc-p-ps.h"
#include "measure.h"

extern u8 add_params_num;			// set_add_data



void interpret()
{
	switch(r_addr)
	{
		case P_ADDR_DCOFF:
		{
			DCDC_OFF;
			break;
		}
		
		case P_ADDR_INCAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKU:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_LESS;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOW:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow -= ADD_LESS;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_INCAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku += ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_DECAKUM:
		{
			x_aku = read_word(EEP_X_AKU);
			x_aku -= ADD_MORE;
			write_word(EEP_X_AKU,x_aku);
			add_params_num = 0;
			break;
		}
		case P_ADDR_INCPOWM:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow += ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		case P_ADDR_DECPOWM:
		{
			x_pow = read_word(EEP_X_POW);
			x_pow -= ADD_MORE;
			write_word(EEP_X_POW,x_pow);
			add_params_num = 1;
			break;
		}
		
		case P_ADDR_L_FACTORY:
		{
			ap_a = 1;
			ap_b = 0;
			write_double(EEP_PRESA,ap_a);
			write_double(EEP_PRESB,ap_b);
			write_double(EEP_S_PRESA,ap_a);
			write_double(EEP_S_PRESB,ap_b);
			break;
		}
		
		case P_ADDR_H_FACTORY:
		{



			
			// 			write_word(EEP_STW1,stw1);
			// 			write_word(EEP_STW2,stw2);

			break;
		}


		
		case P_ADDR_L_RESET:
		{

			break;
		}
		
		case P_ADDR_H_RESET:
		{

			break;
		}
#ifdef MEDIA_PS	
		case P_ADDR_SCALLZ:		// press_abs_low
		{
			write_word(EEP_TMP_FLOAT1,r_data);
			calc_pa();
			write_word(EEP_TMP_FLOAT2,adc_readout);
			break;
		}
		case P_ADDR_SCALLS:		// press_abs_high
		{
			i16 y1 = read_word(EEP_TMP_FLOAT1);
			i16 x1 = read_word(EEP_TMP_FLOAT2);
			i16 y2 = r_data;
			calc_pa();
			i16 x2 = adc_readout;
			
			float ls1 = ((double)(x2-x1))*-y1;
			float rs1 = ((double)(y2-y1))*-x1;
			float y = (x2-x1); 
			 
			ap_b = (ls1 - rs1) / y; 
			ap_a = (y2-y1) / y;
			
			write_double(EEP_PRESA,ap_a);
			write_double(EEP_PRESB,ap_b);
			

			break;
		}
#elif MEDIA_PD
		case P_ADDR_SCALLZ:		// press_abs_low
		{
			write_word(EEP_PRES_VAL_ZERO,r_data);
			calc_pd();
			write_word(EEP_PRESZ,adc_readout);
			break;
		}
		case P_ADDR_SCALLS:		// press_abs_high
		{
			i16 y1 = read_word(EEP_PRES_VAL_ZERO);
			i16 x1 = read_word(EEP_PRESZ);
			write_word(EEP_PRES_VAL_MAX,r_data);
			i16 y2 = r_data;
			calc_pd();
			i16 x2 = adc_readout;
			write_word(EEP_PRESM,x2);		// max
			
			float ls1 = ((double)(x2-x1))*-y1;
			float rs1 = ((double)(y2-y1))*-x1;
			float y = (x2-x1);
			
			ap_b = (ls1 - rs1) / y;
			ap_a = (y2-y1) / y;
			
			write_double(EEP_PRESA,ap_a);
			write_double(EEP_PRESB,ap_b);
			write_double(EEP_S_PRESA,ap_a);
			write_double(EEP_S_PRESB,ap_b);
			break;
		}
		case P_ADDR_PR_ZERO:
		{
			ap_a = read_double(EEP_S_PRESA);
			ap_b = read_double(EEP_S_PRESB);
			
			calc_pd();
			i16 zero = read_word(EEP_PRESZ);
			i16 max = read_word(EEP_PRESM);
			
			i16 diff = zero - (i16)adc_readout;
			zero = adc_readout;
			max -= diff;
			
			float pd = adc_readout * ap_a - ap_b;
			if (pd > 100 || pd < - 100)
			{
				break;
			}
			
			
			//write_word(EEP_PRESZ,zero);
			//write_word(EEP_PRESM,max);
// cal
			i16 y1 = read_word(EEP_PRES_VAL_ZERO);
			i16 x1 = zero;
			i16 y2 = read_word(EEP_PRES_VAL_MAX);
			i16 x2 = max;
			
			float ls1 = ((double)(x2-x1))*-y1;
			float rs1 = ((double)(y2-y1))*-x1;
			float y = (x2-x1);
			
			ap_b = (ls1 - rs1) / y;
			ap_a = (y2-y1) / y;
			
			write_double(EEP_PRESA,ap_a);
			write_double(EEP_PRESB,ap_b);

			
			break;
		}
		
#endif
		
		case P_ADDR_CALLZ:
		{

			
			break;
		}
		case P_ADDR_CALLS:
		{

			break;
		}
		
		case P_ADDR_SCALHZ:
		{

			break;
		}
		case P_ADDR_SCALHS1:
		{

			break;
		}
		case P_ADDR_SCALHS2:
		{

			break;
		}
		
		case P_ADDR_CALHZ:
		{

			break;
		}
		case P_ADDR_CALHS:
		{
			break;
		}
		
		case P_ADDR_RESET_DEFAULTS:
		{
			break;
		}
	}
	
	
}